<?php
/*
Plugin ID: cn.usephp.ask
Plugin Name: 留言板
Plugin URI: http://www.usephp.cn/plugins/ask.html
Description: 为系统增加留言板（问答，咨询，回馈，健议)功能，使网站管理者可以与网站的访问者进行互动。<br/>前台页面需要根据具体情况，添加表单以收集用户的信息.
Author: Leo Ning
Version: 1.0
Author URI: http://www.usephp.cn/
*/
exportcp ( __FILE__, 'models' );

bind ( 'get_desktop_applications', '__the_askmgr_app', 2 );
function __the_askmgr_app($apps) {
	$apps [991] = array ('id' => "askmgr", 'url' => the_plugin_url ( 'ask/' ), 'cls' => 'icon_ask', 'name' => '留言板', 'tip' => '留言板', 'icon' => the_plugin_url ( 'ask/images/ask.png' ) );
	return $apps;
}

bind ( 'desktop_styles', '__the_askmgr_icon' );
function __the_askmgr_icon($icons) {
	$icons .= ".icon_ask { background: url(" . the_plugin_url ( 'ask/images/ask2.png' ) . ") no-repeat 0 50%; }\n";
	return $icons;
}

bind ( 'get_desktop_widgets', '__the_ask_desktop_widget' );
function __the_ask_desktop_widget($widget) {
	$askM = new PlugAskModel ();
	$askes = $askM->where ( array ('deleted' => 0, 'status' => 'new' ) )->count ( true )->limit ( 3 )->retrieve ();
	if ($askes) {
		$plgurl = the_plugin_url ( 'ask' );
		$new_total = $askes->countTotal;
		echo '<div class="stuffbox">
			  		<div class="handlediv" title="点击以切换"><br/></div>
					<h3>留言板(<a href="#" onclick="return openDesktopPanel(\'', $plgurl, '\',\'留言板\',\'icon_ask\',\'askmgr\')">', $new_total, '</a>)</h3>				
					<div class="inside">';
		if ($new_total) {
			foreach ( $askes as $ask ) {
				$ask_content = mb_substr ( $ask ['ask_content'], 0, 26 );
				echo "<p class=\"mgb5\"><span class=\"label label-info\">{$ask['asker_name']}@", date ( "m-d H:i", $ask ['ask_time'] ), "</span>&nbsp;&nbsp;{$ask_content}.<a href=\"{$plgurl}/?Ctlr=ReplyAsk&ask_id={$ask['ask_id']}\" onclick=\"return replyAsk(this)\">回复</a></p>";
			}
		} else {
			echo '<p style="text-align:center">无新留言!</p>';
		}
		if ($new_total > 5) {
			echo '<p style="text-align:right"><a href="#" onclick="return openDesktopPanel(\'', $plgurl, '\',\'留言板\',\'icon_ask\',\'askmgr\')">更多...</a></p>';
		}
		echo '	</div>
			</div>';
	}
	return $widget;
}
bind ( 'desktop_foot', '__the_ask_desktop_foot' );
function __the_ask_desktop_foot($foot) {
	?>
<script type="text/javascript">
	function replyAsk(ask){
		var url = $(ask).attr('href');
		Desktop.openIframeDialog('回复留言', url, function(frame) {
			frame.saveReply(window);
		});
		return false;
	}
</script>
<?php
	return $foot;
}